package com.example.blog.controller.Excel;

import com.example.blog.service.AllToAll.ExcelToWordService;
import com.example.blog.service.AllToAll.WordToPdfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private WordToPdfService wordToPdfService;

    @Autowired
    private ExcelToWordService excelToWordService;

    /**
     * 处理Word到PDF的转换请求，并返回转换后的PDF文件
     * @param file 上传的Word文件
     * @return 转换后的PDF文件
     */
    @PostMapping("/word-to-pdf")
    public ResponseEntity<InputStreamResource> wordToPdf(@RequestParam("file") MultipartFile file) {
        try {
            // 创建临时文件
            File tempFile = File.createTempFile("input", ".docx");
            file.transferTo(tempFile);
            String pdfPath = tempFile.getAbsolutePath().replace(".docx", ".pdf");
            wordToPdfService.convertWordToPdf(tempFile.getAbsolutePath(), pdfPath);

            // 准备返回的PDF文件
            File pdfFile = new File(pdfPath);
            InputStreamResource resource = new InputStreamResource(Files.newInputStream(pdfFile.toPath()));

            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + pdfFile.getName())
                    .contentType(MediaType.APPLICATION_PDF)
                    .contentLength(pdfFile.length())
                    .body(resource);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 处理Excel到Word的转换请求，并返回转换后的Word文件
     * @param file 上传的Excel文件
     * @return 转换后的Word文件
     */
    @PostMapping("/excel-to-word")
    public ResponseEntity<InputStreamResource> excelToWord(@RequestParam("file") MultipartFile file) {
        try {
            // 创建临时文件
            File tempFile = File.createTempFile("input", ".xlsx");
            file.transferTo(tempFile);
            String wordPath = tempFile.getAbsolutePath().replace(".xlsx", ".docx");
            excelToWordService.convertExcelToWord(tempFile.getAbsolutePath(), wordPath);

            // 准备返回的Word文件
            File wordFile = new File(wordPath);
            InputStreamResource resource = new InputStreamResource(Files.newInputStream(wordFile.toPath()));

            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + wordFile.getName())
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .contentLength(wordFile.length())
                    .body(resource);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
    }
}
